﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MayhemCore;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using System.Windows.Threading;
using Total_Mayhem.Common;
//using TaskSchedulerEngine.Fluent;

namespace Total_Mayhem.Other
{
    /* DISABLED
    [DataContract]
    [MayhemModule("Date/Time Event", "Create a Date / Time event with options for repeating.")]
    */
    public class DateTimeEvent : EventBase, IWpfConfigurable
    {

        #region Initialization Code
        protected override void OnLoadDefaults()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnLoadFromSaved()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnBeforeLoad()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnAfterLoad()
        {
            Misc.WriteLog("Entered");
            //timer = new DispatcherTimer();
            //timer.Tick += Timer_Tick;
            //setInterval(); Will be called by enable method
            timer = new System.Timers.Timer();
            timer.AutoReset = true;
            timer.Elapsed += Timer_Tick;
        }
        #endregion

        private System.Timers.Timer timer;

        private void setInterval()
        {
            timer.Interval = IntervalinSeconds * 1000;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Misc.WriteLog("Timer fired!");
            //SendPingRequest();
        }

        [DataMember]
        private int IntervalinSeconds;

        protected override void OnEnabling(EnablingEventArgs e)
        {
            //base.OnEnabling(e);
            Misc.WriteLog("Entered");
            setInterval();
            timer.Start();
            
            /*
            if (IntervalinSeconds > 5)
            {
                int intNext = rnd.Next(1, 4);
                Misc.WriteLog("Force-checking in {0} seconds...", intNext);
                Dispatcher.CurrentDispatcher.DelayInvoke(new TimeSpan(0, 0, intNext), SendPingRequest); //Force run check after a few seconds
                //Dispatcher.CurrentDispatcher.Invoke(new Action(SendPingRequest), DispatcherPriority.ApplicationIdle,null); 
            }*/
        }

        protected override void OnDisabled(DisabledEventArgs e)
        {
            //base.OnDisabled(e);
            Misc.WriteLog("Entered");
            timer.Stop();
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                Misc.WriteLog("OpenConfig");
                return new DateTimeEventConfig();
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            DateTimeEventConfig nc = configurationControl as DateTimeEventConfig;
            if (nc != null)
            {
                Misc.WriteLog("Settings: ");
                //this.WaitinMilliSeconds = nc.WaitinMilliSeconds;
                ScheduleTask();
            }
        }

        private void ScheduleTask()
        {
            //var s = new Schedule().WithLocalTime().Execute<TaskSchedulerEngine.ConsoleWriteTask>("ASDF");
        }

        /// <summary>
        /// Used to generate the text that is displayed on the main page of events/triggers to describe this entry
        /// </summary>
        /// <returns></returns>
        public string GetConfigString() { return string.Format("{0}", DateTime.Now); }

    }
}
